package com.jxthxr.monitor.utils;

import com.alibaba.fastjson.JSONObject;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @Author silence.Z
 * @Date 2022/6/10 11:00
 * @PackageName:com.jxthxr.monitor.utils
 * @ClassName: DingDingMessageUtil
 * @Description: TODO
 * @Version 1.0
 */
public class DingDingMessageUtil {
	public static String access_token = "186f2efa87ea4a60df33e125bc384412971939c91865c666a8fa54389351fd9c";

	public DingDingMessageUtil() {
	}

	public static void sendTextMessage(String msg) {
		try {
			Message message = new Message();
			message.setMsgtype("text");
			message.setText(new MessageInfo(msg));
			URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
			conn.connect();
			OutputStream out = conn.getOutputStream();
			String textMessage = JSONObject.toJSONString(message);
			byte[] data = textMessage.getBytes();
			out.write(data);
			out.flush();
			out.close();
			InputStream in = conn.getInputStream();
			byte[] data1 = new byte[in.available()];
			in.read(data1);
			System.out.println(new String(data1));
		} catch (Exception var9) {
			var9.printStackTrace();
		}

	}
}
